package edu.cuit.canary.commons.oss.config;

import edu.cuit.canary.commons.oss.service.FileService;
import edu.cuit.canary.commons.oss.service.support.FileServiceBean;
import io.minio.MinioClient;
import jakarta.annotation.Resource;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
//@EnableAutoConfiguration
@EnableConfigurationProperties(MinioConfigProperties.class)
//当引入FileService接口时,自动装配bean
@ConditionalOnClass(FileService.class)
public class MinioConfig {
    @Resource private MinioConfigProperties minioConfigProperties;
    /**
     * 配置MinioClient
     */
    @Bean
    public MinioClient minioClient(){
        return MinioClient.builder()
                .credentials(minioConfigProperties.getAccessKey(), minioConfigProperties.getSecretKey())
                .endpoint(minioConfigProperties.getEndpoint())
                .build();
    }

    @Bean
    @ConditionalOnMissingBean
    public FileService fileService(){
        return new FileServiceBean();
    }
}
